Component({
  properties: {
    dataList: {
      type: Object,
      value: {}
    }
  },
  data: {
    topBanner: '/image/images/question.png', // 注意路径可能需要调整
    chooseonImg: '/image/images/chooseon.png', // 注意路径可能需要调整
    chooseonImg2: '/image/images/chooseon2.png', // 注意路径可能需要调整
    totalNum: 0,
    currentIndex: 0,
    newQuestionsAnswer: [],
    formSubmitData: []
  },
  observers: {
    'dataList': function (obj) {
      console.log(obj,'obj')
      if (obj.questionList?.length) {
        console.log('1231313')
        this.setData({
          newQuestionsAnswer: obj.questionList,
          totalNum: obj.questionList.length
        });
        console.log(this.data.newQuestionsAnswer,'newArr')
        this.setEmptyData();
      }
    }
  },
  methods: {
    setEmptyData() {
      this.data.newQuestionsAnswer.forEach((res, index) => {
        let userAnswer = '';
        if (res.optionList) {
          res.optionList.forEach(item => {
            if (!item.isSelect) item.isSelect = false;
          });
          console.log(res,'optionList')
          userAnswer = res.optionList.filter(item => item.isSelect).map(item => item.optionValue).join(',');
        } else if (res.problemType === 'question') {
          userAnswer = res.userAnswer || '';
        }
        this.data.formSubmitData.push({
          paperId:res.paperId,
          questionsId: res.questionsId,
          userAnswer: userAnswer
        });
      });
      this.setData({
        formSubmitData: this.data.formSubmitData
      });
    },
    submitData() {
      for (let i = 0; i < this.data.formSubmitData.length; i++) {
        if (!this.data.formSubmitData[i].userAnswer) {
          wx.showToast({
            title: '请完成第' + (i + 1) + '题后提交！',
            icon: 'none'
          });
          this.setData({
            currentIndex: i
          });
          return;
        }
      }
      this.triggerEvent('submit', this.data.formSubmitData);
    },
    singChoose(e) {
      // index 题目下标
      // idxs 选项下标
      const { index, idxs } = e.currentTarget.dataset;
      // console.log(e,'eeeeeeee')
      let userAnswer = '';
      const item = this.data.newQuestionsAnswer[index].optionList;
      console.log(item)
      item.map((child,childIndex)=>{
        if(childIndex===idxs){
          userAnswer = child.optionValue;
          return child.isSelect = true
        }else{
          return child.isSelect = false
        }
      })
      this.setData({
        [`newQuestionsAnswer[${index}].optionList`]: item,
        [`formSubmitData[${index}]`]: {paperId:this.data.newQuestionsAnswer[index].paperId,questionsId:this.data.newQuestionsAnswer[index].questionsId,userAnswer:userAnswer}
      });
      // console.log(this.data.newQuestionsAnswer[index].optionList)
      console.log(this.data.formSubmitData[index])
    },
    multyChoose(e) {
      const { index, idxm } = e.currentTarget.dataset;
      const item = this.data.newQuestionsAnswer[index].optionList[idxm];
      console.log('item',item)
      item.isSelect = !item.isSelect;
      let userAnswer = this.data.newQuestionsAnswer[index].optionList
        .filter(child => child.isSelect)
        .map(child => child.optionValue);
        
      this.data.formSubmitData[index].userAnswer = userAnswer.join(',');
      this.setData({
        newQuestionsAnswer: JSON.parse(JSON.stringify(this.data.newQuestionsAnswer)),
        formSubmitData: JSON.parse(JSON.stringify(this.data.formSubmitData))
      });
      console.log(this.data.newQuestionsAnswer[index].optionList)
      console.log(this.data.formSubmitData[index])
    },
  bindTextAreaBlur(e) {
    const { index } = e.currentTarget.dataset;
    const { value } = e.detail;
    const { newQuestionsAnswer, formSubmitData } = this.data;

    const updatedQuestions = [...newQuestionsAnswer];
    updatedQuestions[index].userAnswer = value;

    const updatedFormSubmitData = [...formSubmitData];
    updatedFormSubmitData[index].userAnswer = value;

    this.setData({
      newQuestionsAnswer: updatedQuestions,
      formSubmitData: updatedFormSubmitData
    });
  },
  eventHandle(e) {
    this.setData({ currentIndex: e.detail.current });
  },
  back(e) {
    const { index } = e.currentTarget.dataset;
    if(index==0){
      wx.showToast({
        title: '这是第一题',
        icon:'none'
      })
      return
    }
    this.setData({
      currentIndex:index-1
    })
  },
  next(e) {
    const { index } = e.currentTarget.dataset;
    if(index==this.data.dataList.length){
      wx.showToast({
        title: '这是最后一题',
        icon:'none'
      })
      return
    }
    this.setData({
      currentIndex:index+1
    })
  }
}
})